<?php
/**
 * 主题专区
 *
 * 主题专区controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\TypesService\TypesService;
use Illuminate\Support\Facades\Response;

class TypesController extends Controller
{
    protected $typesService;
    public function __construct(TypesService $typesService)
    {
        $this->typesService = $typesService;
    }

    /**
     * 获取所有主题的产品
     * @param Request $request
     * @return mixed
     */
    public function postAll(Request $request){
        $check_data = array('user_id'=>'integer');
        $v = $this->typesService->store($request,$check_data);
        if ($v->fails())
        {
            if ($v->errors()->first('user_id')) {
                $resultMes = $this->typesService->createResult('M07001',(object)array());
                return Response::json($resultMes);
            }
        }
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) <= 0 ? 10 : intval($request->input('per_page'));
        //获取所有
        $result = $this->typesService->getList($per_page);
        return Response::json($result);
    }

    /**
     * 获取某主题的产品
     * @param Request $request
     * @return mixed
     */
    public function postGetTypeProducts(Request $request){
        $check_data = array(
            'user_id'=>'integer',
            'type_id'=>'required|integer|min:1',
            'app_id'=>'required|integer'
        );
        $v = $this->typesService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('user_id')){
                $resultMes = $this->typesService->createResult('M07001',(object)array());
            }else if($v->errors()->first('app_id')){
                $resultMes = $this->typesService->createResult('M07001',(object)array());
            } else{
                $resultMes = $this->typesService->createResult('M07003',(object)array());
            }
            return Response::json($resultMes);
        }
        $type_id = intval($request->input('type_id'));
        $per_page = intval($request->input('per_page')) > 0 ? intval($request->input('per_page')) : 10;
        $result = $this->typesService->getProductListByTypeId($type_id,$per_page);
        return Response::json($result);
    }
}
